<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 个人信息 -->
    <input type="file" hidden ref="file" @change="onFileChange">
    <van-cell class="photo-cell" title="头像" is-link center
    @click="$refs.file.click()">
      <van-image class="avatar" round fit="cover" :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      is-link
      :value="user.name"
      @click="isUpdateNameShow = true"
    />
    <van-cell title="性别" :value="user.gender === 0 ? '男' : '女'" is-link 
    @click=" isUpdateGenderShow=true"/>
    <van-cell title="生日" :value="user.birthday"
    @click="isUpdateBirthdayShow=true" is-link />
    <!-- /个人信息 -->
    <van-popup
      v-model="isUpdateNameShow"
      style="height: 100%"
      position="bottom"
    >
      <update-name
        @close="isUpdateNameShow = false"
        v-if="isUpdateNameShow"
        v-model="user.name"
      />
    </van-popup>
    <van-popup
    v-model="isUpdateGenderShow"
    position="bottom"
>
   <update-gender
        @close="isUpdateGenderShow = false"
        v-model="user.gender"
      />
</van-popup>
<van-popup
    v-model="isUpdateBirthdayShow"
    position="bottom"
>
 <update-birthday
        @close="isUpdateBirthdayShow = false"
        v-model="user.birthday"
        v-if="isUpdateBirthdayShow"
      />
</van-popup>
<van-popup
    v-model="isUpdatePhotoShow"
    position="bottom"
    style="height: 100%;"
>
  <update-photo
        v-if="isUpdatePhotoShow"
        :img="img"
        @close="isUpdatePhotoShow = false"
        @update-photo="user.photo=$event"
      />
</van-popup>
  </div>
</template>

<script>
import { getUserProfile } from "@/api/user";
import UpdateGender from './components/update-gender'
import UpdateName from "./components/update-name";
import UpdateBirthday from './components/update-birthday'
import UpdatePhoto from './components/update-photo'



export default {
  name: "UserProfile",
  components: {
    UpdateName,
     UpdateGender,
      UpdateBirthday,
       UpdatePhoto
  },
  props: {},
  data() {
    return {
      user: {},
      isUpdateNameShow: false,
      isUpdateGenderShow:false,
      isUpdateBirthdayShow: false,
      isUpdatePhotoShow:false,
      img:null,
    };
  },
  computed: {},
  watch: {},
  created() {
    this.loadUserProfile();
  },
  mounted() {},
  methods: {
    async loadUserProfile() {
      try {
        const { data } = await getUserProfile();
        this.user = data.data;
      } catch (error) {
        this.$toast("数据获取失败");
      }
    },
    onFileChange(){
        const file=this.$refs.file.files[0]
       this.img=window.URL.createObjectURL(file)
       this.isUpdatePhotoShow=true
      this.$refs.file.value=""
    }
  },
};
</script>

<style scoped lang="less">
.user-profile {
  .avatar-cell {
    .van-cell__value {
      display: flex;
      flex-direction: row-reverse;
    }
    .avatar {
      width: 60px;
      height: 60px;
    }
  }
  .van-popup {
    background-color: #f5f7f9;
  }
}
</style>